package com.chefu.system.controller;


import com.chefu.common.exception.CfException;
import com.chefu.common.exception.ExceptionEnum;
import com.chefu.common.pojo.PageResult;
import com.chefu.common.pojo.R;
import com.chefu.system.pojo.Dept;
import com.chefu.system.service.DeptService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/system/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 分页查询部门
     *
     * @param page     当前页
     * @param pageSize 页面大小
     * @param deptNum  模糊查询字段
     * @param deptName 模糊查询字段
     * @return 分页数据
     */
    @GetMapping("/list")
    public R findByPage(
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(name = "deptNum", required = false) String deptNum,
            @RequestParam(name = "deptName", required = false) String deptName
    ) {
        PageResult<Dept> pageResult = deptService.findByPage(page, pageSize, deptNum, deptName);
        return R.ok().data("pageResult", pageResult);
    }

    /**
     * 添加部门
     *
     * @param dept          添加对象
     * @param bindingResult 封装了所有字段验证失败的错误信息
     * @return
     */
    @PostMapping("/insert")
    public R insertDept(@Valid Dept dept, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            //取出错误信息
            String errorMsg = bindingResult.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("|"));
            System.out.println(errorMsg);
            throw new CfException(5000, errorMsg);
        }
        deptService.insertDept(dept);
        return R.ok();
    }

    /**
     * 根据部门id查询部门信息
     *
     * @param id
     * @return
     */
    @GetMapping("/query")
    public R findByDeptId(@RequestParam("id") Long id) {
        Dept dept = deptService.findByDeptId(id);
        return R.ok().data("dept", dept);
    }

    /**
     * 修改部门
     *
     * @param dept          修改对象
     * @param bindingResult
     * @return
     */
    @PutMapping("/update")
    public R updateDept(@Valid Dept dept, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            //取出错误信息
            String errorMsg = bindingResult.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("|"));
            System.out.println(errorMsg);
            throw new CfException(5000, errorMsg);
        }
        deptService.updateDept(dept);
        return R.ok();
    }

    /**
     * 根据id删除部门
     * @param id 部门id
     * @return
     */
    @DeleteMapping("/delete")
    public R delDept(@RequestParam("id") Long id) {
        try {
            deptService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
        return R.ok();
    }


}

